package com.zenithsun.web;

import com.zenithsun.jwtshiro.util.JWTUtil;
import com.zenithsun.sys.user.dao.UserMapper;
import com.zenithsun.sys.user.domain.bo.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 *
 * @version 1.0.0
 * @blog http://blog.didispace.com
 *
 */
@Controller
public class HelloController {
    private static Logger logger= LoggerFactory.getLogger(HelloController.class);
    @Autowired
    private UserMapper userMapper;
    @ResponseBody
    @RequestMapping(value = "/hello",method= RequestMethod.POST)
    @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String"),
    })
    public String hello(@PathVariable String  id ) {
        String result="";
        User user =new User();

        //userMapper.insert(user);
        List<User> userList = userMapper.findByName("karlwang");

        return result;
    }
    @ApiIgnore
    @RequestMapping(value = "/index" ,method= RequestMethod.GET)
    //@RequiresRoles(logical = Logical.OR, value = {"user", "admin"})
    @ResponseBody
    public String index(ModelMap map)  throws Exception {
        map.addAttribute("host", "http://blog.didispace.com");
        //throw new Exception("发生错误2");
        if(logger.isDebugEnabled() ) {
            logger.info("收集日志");
        }
        logger.debug("ss");
        return "index";
    }
    @RequestMapping("/doLogin")
    @ResponseBody
    public ResponseEntity doLogin(String username, String password) {
       return ResponseGenerator.genSuccessResult(JWTUtil.sign("liming","123"));
    }

    @RequestMapping("/doLoginOut")
    @ResponseBody
    public ResponseEntity doLoginOut(String username, String password) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return ResponseGenerator.genSuccessResult("登出成功");
    }

    @RequestMapping("/article")
    @ResponseBody
    public ResponseEntity article() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            return  ResponseGenerator.genSuccessResult("成功");
        } else {
            return ResponseGenerator.genUnauthorizedResult();
        }
    }
}